package com.atguigu.distributed.lock.leecode;

import java.util.HashMap;
import java.util.Map;

public class LRUCacheDemo2 {
    class Node<K,V>{
        K key;
        V value;
        Node<K,V> prev;
        Node<K,V> next;
        public Node(){
            this.prev = this.next = null;
        }
        public Node(K key,V value){
            this.key = key;
            this.value = value;
            this.prev = this.next = null;
        }
    }
    class DoubledLinkedList<K,V>{
        Node<K,V> head;
        Node<K,V> tail;
        public DoubledLinkedList(){
            head = new Node<>();
            tail = new Node<>();
            head.next = tail;
            tail.prev = null;
        }
        public void addHead(Node<K,V> node){
            node.next = head.next;
            node.prev = head;
            head.next.prev = node;
            head.next = node;
        }
        public void removeNode(Node<K,V> node){
            node.next.prev = node.prev;
            node.prev.next = node.next;
            node.next = null;
            node.prev = null;
        }
        public Node getLast(){
            return tail.prev;
        }
    }
    private int cacheSize;
    Map<Integer,Node<Integer,Integer>> map;
    DoubledLinkedList<Integer,Integer> doubledLinkedList;
    public LRUCacheDemo2(int size){
        this.cacheSize = size;
        map = new HashMap<>();
        doubledLinkedList = new DoubledLinkedList<>();
    }
    public int get(int key){
        if(!map.containsKey(key)){
            return -1;
        }
        Node<Integer, Integer> node = map.get(key);
        doubledLinkedList.removeNode(node);
        doubledLinkedList.addHead(node);
        return node.value;
    }
    public void put(int key,int value){
        if(map.containsKey(key)){
            Node<Integer, Integer> node = map.get(key);
            node.value = value;
            map.put(key, node);
            doubledLinkedList.removeNode(node);
            doubledLinkedList.addHead(node);
        }else{
            if(map.size() == cacheSize){
                Node lastNode = doubledLinkedList.getLast();
                map.remove(lastNode.key);
                doubledLinkedList.removeNode(lastNode);
            }
        }
        map.put(key, new Node<>(key, value));
        doubledLinkedList.addHead(map.get(key));
    }

    public static void main(String[] args) {
        LRUCacheDemo2 lruCacheDemo = new LRUCacheDemo2(3);
        lruCacheDemo.put(1, 1);
        lruCacheDemo.put(2, 2);
        lruCacheDemo.put(3, 3);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(4, 1);
        System.out.println(lruCacheDemo.map.keySet());

        lruCacheDemo.put(3, 1);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(3, 1);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(3, 1);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(5, 1);
        System.out.println(lruCacheDemo.map.keySet());
    }
}
